﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Microsoft.Win32;

using TouchControls;

namespace TouchExplorer.Controls
{
    /// <summary>
    /// Interaction logic for ClipboardFooter.xaml
    /// </summary>
    public partial class ClipboardFooter : Canvas
    {
        public bool Expanded { get; set; }

        public ClipboardFooter()
        {
            InitializeComponent();
            Header.AddHandler(Grid.MouseUpEvent, new MouseButtonEventHandler(HeaderMouseUp), true);
            Loaded += (loadedsender, loadedarg) =>
                {
                    App.Current.Clipboard.ClipboardElements.CollectionChanged += (sender, arg) =>
                        {
                            if (App.Current.Clipboard.Count == 0)
                                this.Collapse();
                        };
                };
        }

        void HeaderMouseUp(object sender, MouseButtonEventArgs arg)
        {
            Toggle();
        }

        void ClearClipboard(object sender, RoutedEventArgs arg)
        {
            App.Current.Clipboard.Clear();
        }

        private void ClipboardPaste(object sender, RoutedEventArgs e)
        {
            var model = DataContext as ExplorerViewModel;

            var dir = model.CurrentDirectory;
            if (dir != null)
            {
                App.Current.Clipboard.Apply(dir.FullPath);
            }
        }

        private Window _parentwindow;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _parentwindow = this.FindParent<Window>();
            _parentwindow.AddHandler(Window.MouseUpEvent, new MouseButtonEventHandler(WindowMouseUp), true);
        }

        void WindowMouseUp(object sender, MouseButtonEventArgs arg)
        {
            //TODO collapse panel if clicked outside footer
        }

        private void Toggle()
        {
            if (Expanded)
            {
                this.Collapse();
            }
            else
            {
                this.Expand();
            }
        }

        private void Expand()
        {
            if (!Expanded)
            {
                var stb = (Storyboard)this.FindResource("ExpandStb");
                var anim = (DoubleAnimation)stb.Children[0];
                anim.To = -this.Content.RenderSize.Height;
                stb.Begin();
                this.Expanded = true;
            }
        }

        private void Collapse()
        {
            if (Expanded)
            {
                var stb = (Storyboard)this.FindResource("CollapseStb");
                stb.Begin();
                this.Expanded = false;
            }
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Width = constraint.Width;
            if (_parentwindow != null)
            {
                Width = _parentwindow.Width;
                Content.MaxHeight = _parentwindow.Height / 2;
            }
            Layout.Width = Width;
            Layout.InvalidateMeasure();
            
            //Expanded = false;
            //TranslateFooter.Y = 0;
            var res = base.MeasureOverride(constraint);
            return new Size(Width, res.Height);
        }

        //protected override Size ArrangeOverride(Size arrangeSize)
        //{
        //    Width = arrangeSize.Width;
        //    Height = Layout.Height + 50;
        //    var parentwindow = this.FindParent<Window>();
        //    if (parentwindow != null)
        //    {
        //        Width = parentwindow.Width;
        //    }
        //    var res = base.ArrangeOverride(arrangeSize);
        //    Layout.UpdateLayout();
        //    return new Size(Width, res.Height);
        //}
    }
}
